<script>
	import QQMapWX from "common/qqmap-wx-jssdk.min.js";
	import {getOpenIDAction, WeChatOpenID,WeChatMember,WXAddAction,WXAppStyle} from "./api/global.js";
	export default {
		data() {
			return {
				memberInfo: {
					//用户表
					Member_SerialNumber: "",
					Nick: "游客",
					Image: "https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0",
					Member_Name: "",
					Mobile: "",
					Head_URL: "",
					Open_ID: "",
					Sales_ID: "",
					Login_authorization: 0,
					Rights_Type: 0,
					Dealer_Authority: "",
					Employee_Authority: 0,
					App_ID: uni.getAccountInfoSync().miniProgram.appId,
					Appoint_Member_Type: 0,
					Enterprise_Info_SerialNumber: "E00000000000016",
					Children: {
						member_account_info: [{
							Member_Account_SerialNumber: "",
							Member_SerialNumber: "",
							Account_Type: 0,
							Prepayment: 0,
							Prepayment_Sales: 0,
							Prepayment_Balance: 0,
							Gift_Amount: 0,
							Gift_Sales: 0,
							Gift_Balance: 0,
							Sales_Point: 0,
							Sales_Point_Sales: 0,
							Sales_Point_Balance: 0,
							Gift_Point: 0,
							Gift_Point_Sales: 0,
							Gift_Point_Balance: 0,
						}, ],
					},
				},
				position: {},
			};
		},
		globalData: {
			//全局变量
			// 小正
			// Domain_Name: 'https://3451m7y168.goho.co/svhl',
			//Domain_IMG: 'https://3451m7y168.goho.co/svhl/img',
			//大正
			Domain_Name: "https://digital.essc.top/svhl",
			Domain_IMG: "https://digital.essc.top/dist/img1", //图片域名
			Domain_ICON: "https://digital.essc.top/dist/hl_icon", //图标
			openid: "", //用户openid
			//appid: uni.getAccountInfoSync().miniProgram.appId, //小程序appid
			appid: 'wx752cd7c5466abbaf',
			enterprise: "E00000000000016", //企业编号
			memberInfo: {}, //用户信息
			WeChatCss: {}, //小程序样式
			token: "",
			userlocation: "",
			switch_index: 0, //身份索引
			Member_Share_SerialNumber:'',
		},

		onLaunch: async function() {
			console.log("保佑我的代码在正式环境中运行不要出现问题~~");
			const _this = this;
			const App_ID = this.$scope.globalData.appid;
			const location = await this.getLocationInfo();
			this.position = location.address;
			//获取opid
			uni.login({
				provider: "weixin",
				success: function(loginRes) {
					// let town = uni.getStorageSync('location').data.town;
					_this.WXOpenID(loginRes.code, App_ID, (openid) => {
						_this.$scope.globalData.openid = openid;
						_this.memberInfo.Open_ID = openid;
						uni.setStorageSync("openid", openid);
						uni.setStorageSync("Enterprise_Info_SerialNumber", _this.$scope.globalData.enterprise);
						uni.setStorageSync("App_ID", App_ID);
						_this.WXMember(_this.$scope.globalData.enterprise,App_ID,openid, (data) => {
								if (data.length == 0) {
									//3_1.添加
									_this.Add(JSON.stringify({member_info: [_this.memberInfo]}), openid, (res) => {
										_this.$scope.globalData.memberInfo = _this.memberInfo;
										if (res.Status == "success") {
											uni.showModal({
												title: "提示",
												content: "是否完善个人信息",
												success: function(res) {
													//5_2跳转到个人信息详情页
													if (res.confirm) {
														//跳转到页面
														uni.navigateTo({
															url: "/pages/my/personal_information",
														});
													} else if (res.cancel) {
														//5_1关闭弹窗
													}
												},
											});
										}
									});
								} else {
									//3_2.用户信息存到全局变量
									_this.$scope.globalData.memberInfo = data[0];
									if (data[0].Dealer_Authority != "") {
										_this.$scope.globalData.switch_index = 1;
										uni.redirectTo({
											url: "/pages/Merchant/index",
										});
									}
								}
							}
						);
					});
				},
			});
		},

		methods: {
			watch: function(method, istr) {
				var obj = this.globalData;
				Object.defineProperty(obj, istr, {
					configurable: true,
					enumerable: true,
					set: function(value) {
						this._consumerGoodsStatus = value;
						method(value);
					},
					get: function(value) {
						return this._consumerGoodsStatus;
					},
				});
			},
			WXAppStyle: async function(data) {
				const res = await WXAppStyle(data);
				if (res.Status == "success") {
					this.$scope.globalData.WeChatCss = res.Data;
				}
			},
			WXOpenID: async function(Code, App_ID, callback) {
				//原代码
				// const res = await WeChatOpenID({Code,Address});
				// uni.setStorageSync('Expiration_time',parseInt(new Date().getTime() / 1000) + 1800);
				// if (res.enterprise_info.length == 0) {
				//   if (uni.getStorageSync("Expiration_time") < parseInt(new Date().getTime() / 1000)) {
				//         uni.showModal({
				//         title: "提示",
				//         content: "当前附件暂无店铺，使用默认店铺",
				//         success: function (res) {
				//           //5_2跳转到个人信息详情页
				//           if (res.confirm) {
				//             //跳转到页面
				//           } else if (res.cancel) {
				//             //5_1关闭弹窗
				//           }
				//         },
				//         });
				//     }
				//   }else{
				//     this.$scope.globalData.enterprise = res.enterprise_info[0].enterprise;
				//   }
				// if (res.Status == "success") {
				//     callback(res.openid,res.enterprise_info);
				// }
				//新代码
				const res = await getOpenIDAction({Code,App_ID,Open_ID:'获取Open_ID'});
				if (res.Status == "success") {
				    callback(res.Open_ID);
				}
				// callback('oDXqN6_1pfpJoBkPgABc7LubQsLw', []);
			},
			WXMember: async function(enterprise, App_ID, Open_ID, callback) {
				callback(
					await WeChatMember({
						enterprise,
						App_ID,
						Open_ID,
					})
				);
			},
			Add: async function(Data, Open_ID, callback) {
				callback(await WXAddAction({
					Data,
					Open_ID,
				}));
			},
			//获取坐标
			async getLocationInfo() {
				const _this = this;
				return new Promise((resolve) => {
					//位置信息默认数据
					let location = {
						longitude: 0,
						latitude: 0,
						province: "",
						city: "",
						area: "",
						street: "",
						address: "",
					};
					uni.getLocation({
						type: "gcj02",
						success(res) {
							location.longitude = res.longitude;
							location.latitude = res.latitude;
							// 腾讯地图Api
							const qqmapsdk = new QQMapWX({
								key: "IH2BZ-LHWC5-FJDI3-I6ITE-SUKD3-NMF4B", //这里填写自己申请的key
							});
							qqmapsdk.reverseGeocoder({
								location,
								success(response) {
									let info = response.result;
									location.province = info.address_component.province;
									location.city = info.address_component.city;
									location.area = info.address_component.district;
									location.street = info.address_component.street;
									location.address = info.address;
									location.name = info.address_reference.landmark_l2.title,
									location.town = info.address_reference.town.title
									resolve(location);
									let time = parseInt(new Date().getTime() / 1000) + 1800;
									uni.setStorageSync("location", {
										data: location,
										expires_in: time,
									});
								},
							});
						},
						fail(err) {
							resolve(location);
						},
					});
				});
			},

		},

		onShow: function() {},
		onHide: function() {
			console.log("App Hide");
		},
	};
</script>
<style lang="scss">
	/*每个页面公共css */
	@import "static/css/free.css";
	@font-face {
		font-family: iconfont;
		src: url('static/css/iconfont.ttf');
	}
	page {
		height: 100%;
	}
</style>